package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.enums.CodeAndMessageEnum;
import com.briup.exception.SystemException;
import com.briup.mapper.LogMapper;
import com.briup.pojo.Log;
import com.briup.result.PageTemplate;
import com.briup.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * @author Lenovo
 * @description 针对表【pts_log(日志表)】的数据库操作Service实现
 * @createDate 2024-03-13 09:19:46
 */
@Service
@Slf4j
public class LogServiceImpl extends ServiceImpl<LogMapper, Log>
        implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Override
    public PageTemplate queryPageList(Integer pageNum, Integer pageSize, String originalIp) throws SystemException {
        //参数校验
        if (Objects.isNull(pageNum) || Objects.isNull(pageSize)) {
            //页码或页面大小为空
            throw new SystemException(CodeAndMessageEnum.PAGE_NUMBER_OR_PAGE_SIZE_IS_NULL);
        }
        //创建分页对象
        Page<Log> logPage = new Page<>(pageNum, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Log> logLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //封装条件
        logLambdaQueryWrapper.eq(Objects.nonNull(originalIp) && StringUtils.hasText(originalIp), Log::getIp, originalIp);
        //查询
        logMapper.selectPage(logPage, logLambdaQueryWrapper);
        PageTemplate pageTemplate = new PageTemplate(logPage.getTotal(),logPage.getRecords());
//        log.info(String.valueOf(pageTemplate));
        //返回模板对象
        return pageTemplate;
    }
}




